﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using GoogleEarthClasses.Entities;

namespace GoogleEarthClasses.Global
{
    public class GEHelper
    {
        #region Constants
        private const string m_NoImage = "http://www.dev-poleposition.com/Images/NoImage.png";
        private const string m_linkImage = "http://www.dev-poleposition.com/Deploy/Website/ImageTable.aspx?PositionId=";
        private const string m_Icon = "http://www.dev-poleposition.com/IconGoogleEarthClassesI.jpg";
        #endregion

        public static void GESetNewPoint(string _Name, string _Latitude, string _Longitude, string _Altitude, string _Description, List<EImage> Images, string _Color)
        {
            EKmlFile _KMLItem;
            string _Code;
            _Code = GEHelper.GeneratePlacemark(_Name, _Latitude, _Longitude, _Altitude, _Description, Images, _Color);
            _KMLItem = new EKmlFile(TypeKMLOfElement.Point, _Name, _Code);
            //m_KMLItems.Add(_KMLItem);
        }


        /// <summary>
        /// Create a KML file
        /// </summary>
        /// <param name="_KMLFile"></param>
        /// <param name="m_Filename"></param>
        public static void WriteKMLFile(string _Content, string m_Filename)
        {
            FileStream _File = new FileStream(m_Filename, FileMode.Create, FileAccess.Write);
            StreamWriter sw = new StreamWriter(_File);
            sw.Write(_Content);
            sw.Close();
            _File.Close();
        }
        /// <summary>
        /// Insert into the kml file information
        /// </summary>
        /// <param name="m_KMLItems"></param>
        /// <returns></returns>
        public static string GenerateKMLFile(ref List<EKmlFile> m_KMLItems)
        {
            string _KMLFile = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                                    "<kml xmlns=\"http://earth.google.com/kml/2.1\">" +
                                    "<Document>" +
                                        "<name id=\"test1\">Poleposition icons</name>";
            foreach (EKmlFile _KMLItem in m_KMLItems){
                _KMLFile += _KMLItem.KMLCode;
            }
            _KMLFile += "</Document>" +
            "</kml>";
            //m_KMLItems = new List<EKmlFile>();
            return _KMLFile;
        }
        /// <summary>
        /// Generate a xml string 
        /// </summary>
        /// <param name="_Name"></param>
        /// <param name="_Latitude"></param>
        /// <param name="_Longitude"></param>
        /// <param name="_Altitude"></param>
        /// <param name="_description"></param>
        /// <param name="_Images"></param>
        /// <returns></returns>
        public static string GeneratePlacemark(string _Name, string _Latitude, string _Longitude, string _Altitude, string _description, List<EImage> _Images, string _Color)
        {
            string _TempImage = "";
            string _Icon = GetPositionIcon(_Color);
            string _AssetHTML = GenerateAssetHTML(_Name, _Latitude, _Longitude, _Altitude, _description, _Images,  _Icon);
            if (_Images.Count > 0){
                foreach (EImage _Image in _Images)
                    _TempImage = "<br><a href='" + Constants.k_linkIdImage + _Image.ImageId.ToString() + "'><img src=\"" + _Image.ContentPath + "\" border=\"0\"/></a>";
            }
            string _Placemark = "";
            _Placemark += "<Placemark>";
            _Placemark += " <Style>";
            _Placemark += "     <BalloonStyle>";
            _Placemark += "         <color>F9DDCD</color>";
            _Placemark += "     </BalloonStyle>";
            _Placemark += " <IconStyle>";
            _Placemark += "     <Icon>";
            _Placemark += "         <href>" + _Icon + "</href>";
            _Placemark += "     </Icon>";
            _Placemark += " </IconStyle>";
            _Placemark += "</Style>";
            _Placemark += "     <name>" + _Name + "</name>";
            _Placemark += "     <description>";
            _Placemark += "         <![CDATA[" + _AssetHTML+ "]]>";
            _Placemark += "     </description>";
            //"<coordinates>-103 16 40.39079,40 37 25.72000,0</coordinates>" +
            _Placemark += "     <Point id=\"test1\">";
            _Placemark += "         <coordinates>" + _Longitude.Replace(",", ".") + "," + _Latitude.Replace(",", ".") + " ," + _Altitude.Replace(",", ".") + "</coordinates>";
            _Placemark += "     </Point>";
            _Placemark += "</Placemark>";
            return _Placemark;
        
        }
        private static string GenerateAssetHTML(string _Name, string _Latitude, string _Longitude, string _Altitude,string _description, List<EImage> _Images, string _Icon)
        {
            bool _Odd = false;
            string sHtmlTemp = "";
            sHtmlTemp += " <table style='font-family:Verdana; font-size:10px; ' border='0'>";
            sHtmlTemp += "  <tr>";
            sHtmlTemp += "      <td valign='top'style='border:0;'>";
            #region ASSET DETAILS COLUMN
            sHtmlTemp += "          <table style='font-family:Verdana; font-size:10px;border:0;'>";
            sHtmlTemp += "              <tr>";
            sHtmlTemp += "                  <td align='center'><img src='" + _Icon + "' border='0'/></td>";
            sHtmlTemp += "              </tr>";
            sHtmlTemp += "              <tr>";
            sHtmlTemp += "                  <td>";
            sHtmlTemp += "                      ID:"+_Name+"<BR>";
            sHtmlTemp += "                      TYPE:XXXX<BR>";
            sHtmlTemp += "                      MAP:XXXX<BR>";
            sHtmlTemp += "                      SEC:XXX";
            sHtmlTemp += "                  </td>";
            sHtmlTemp += "              </tr>";

            sHtmlTemp += "              <tr>";
            sHtmlTemp += "                  <td>";
            sHtmlTemp += "                      <B>LOCATION</B><BR>";
            sHtmlTemp += "                      LAT:" + _Latitude + "<BR>";
            sHtmlTemp += "                      LONG:" + _Longitude + "<BR>";
            sHtmlTemp += "                      ALT:" + _Altitude; 
            sHtmlTemp += "                  </td>";
            sHtmlTemp += "              </tr>";

            sHtmlTemp += "              <tr>";
            sHtmlTemp += "                  <td>";
            sHtmlTemp += "                      <B>LAST VISIT</B><BR>";
            sHtmlTemp += "                      <BR><a href=''>More...</a><BR>";
            sHtmlTemp += "                  </td>";
            sHtmlTemp += "              </tr>";
            sHtmlTemp += "          </table>";
            #endregion ASSET DETAILS COLUMN
            sHtmlTemp += "      </td>";
            sHtmlTemp += "      <td valign='top' border='0'>";
            #region ASSET IMAGES
            sHtmlTemp += "          <table border='0'>";
            sHtmlTemp += "              <tr>";
            foreach (EImage _Image in _Images) {
                if (!_Odd) sHtmlTemp += "          <tr>";
                sHtmlTemp += "          <td style='border:0;'>";
                sHtmlTemp += "               <a href='" + Constants.k_linkIdImage + _Image.ImageId.ToString() + "'>";
                sHtmlTemp += "               <img src=\"" + _Image.ContentPath + "\" border=\"0\"/></a>";
                sHtmlTemp += "          </td>";
                if (_Odd) sHtmlTemp += "          </tr>";
                _Odd = !_Odd;
            }
            if (!_Odd) sHtmlTemp += "          </tr>";
            sHtmlTemp += "          </table>";
            #endregion ASSET IMAGES
            sHtmlTemp += "      </td>";
            sHtmlTemp += "  </tr>";
            sHtmlTemp += "  <tr border='0'>";
            sHtmlTemp += "      <td colspan='2'><a href=''>Directions from Corporate Office</a> </td>";
            sHtmlTemp += "  </tr>";
            sHtmlTemp += "</table>";
            return sHtmlTemp;
        
        }

        /// <summary>
        /// Generate a xml string with the style for the type of polygon
        /// </summary>
        public static string GeneratePolygonTypeStyle(EPolygonType _PolygonType)
        {
            string _Style = "";
            _Style += "<Style id=\"" + _PolygonType.TypeName + "\">";
            _Style += "     <LineStyle>";
            _Style += "         <width>" + _PolygonType.LineWidth + "</width>";
            _Style += "     </LineStyle>";
            _Style += "     <PolyStyle>";
            _Style += "         <color>" + _PolygonType.LineRgb + "</color>";
            _Style += "     </PolyStyle>";
            _Style += "</Style>";
            return _Style;
        }

        /// <summary>
        /// Generate a xml string with the polygon
        /// </summary>
        public static string GeneratePolygon(string poligonID, string poligonName, string poligonType, string poligonPoints)
        {
            string _Placemark = "";
            _Placemark += "<Placemark>";
            _Placemark += "     <name>" + poligonID + "_" + poligonName + "</name>";
            _Placemark += "     <styleUrl>#" + poligonType + "</styleUrl>";
            _Placemark += "     <Polygon>";
            _Placemark += "         <extrude>1</extrude>";
            _Placemark += "         <altitudeMode>relativeToGround</altitudeMode>";
            _Placemark += "         <outerBoundaryIs>";
            _Placemark += "         <LinearRing>";
            _Placemark += "         <coordinates>";
            _Placemark += poligonPoints;
            _Placemark += "         </coordinates>";
            _Placemark += "         </LinearRing>";
            _Placemark += "         </outerBoundaryIs>";
            _Placemark += "     </Polygon>";
            _Placemark += "</Placemark>";
            return _Placemark;
        }

        /// <summary>
        /// Generate a xml string with the polygon
        /// </summary>
        public static string GeneratePolygon(EPolygon _Polygon)
        {   string _Placemark = "";
            _Placemark += "<Placemark>";
            _Placemark += "     <name>" + _Polygon.PolygonId.ToString() + "_" + _Polygon.Name + "</name>";
            _Placemark += "     <styleUrl>#" + _Polygon.TypeName + "</styleUrl>";
            _Placemark += "     <Polygon>";
            _Placemark += "         <extrude>1</extrude>";
            _Placemark += "         <altitudeMode>relativeToGround</altitudeMode>";
            _Placemark += "         <outerBoundaryIs>";
            _Placemark += "         <LinearRing>";
            _Placemark += "         <coordinates>";
            _Placemark +=                _Polygon.Points;
            _Placemark += "         </coordinates>";
            _Placemark += "         </LinearRing>";
            _Placemark += "         </outerBoundaryIs>";
            _Placemark += "     </Polygon>";
            _Placemark += "</Placemark>";
            return _Placemark;
        }
        /// <summary>
        /// Generate a xml string for a line between two poles
        /// </summary>
        public static string GenerateLine(string SourceLatitude, string SourceLongitude, string DestinationLatitude, string DestinationLongitude)
        {
            string _Placemark = "";
            _Placemark += "<Placemark>";
            _Placemark += "     <name>extruded</name>";
            _Placemark += "     <styleUrl>#msn_LineStyle</styleUrl>";
            _Placemark += "     <LineString>";
            _Placemark += "         <extrude>1</extrude>";
            _Placemark += "         <tessellate>1</tessellate>";
            _Placemark += "         <altitudeMode>relativeToGround</altitudeMode>";
            _Placemark += "         <coordinates>";
            _Placemark += SourceLongitude.Replace(",", ".") + "," + SourceLatitude.Replace(",", ".") + ",0 " + DestinationLongitude.Replace(",", ".") + "," + DestinationLatitude.Replace(",", ".")+",0";
            _Placemark += "         </coordinates>";
            _Placemark += "     </LineString>";
            _Placemark += "</Placemark>";
            return _Placemark;
        }
        public static string GenerateLineStyle() 
        { 
            string _Placemark = "<Style id=\"sh_LineStyle\">";
            _Placemark += "<IconStyle>";
		    _Placemark +="	<scale>1.2</scale>";
		    _Placemark +="</IconStyle>";
		    _Placemark +="<LineStyle>";
		    _Placemark +="  <color>ffff00aa</color>";
		    _Placemark +="	<width>5</width>";
		    _Placemark +="</LineStyle>";
		    _Placemark +="<PolyStyle>";
		    _Placemark +="<color>ff00aa00</color>";
		    _Placemark +="</PolyStyle>";
	        _Placemark +="</Style>";
	        _Placemark +="<StyleMap id=\"msn_LineStyle\">";
		    _Placemark +="<Pair>";
		    _Placemark +="<key>normal</key>";
		    _Placemark +="<styleUrl>#sn_LineStyle</styleUrl>";
	        _Placemark +="</Pair>";
		    _Placemark +="<Pair>";
		    _Placemark +="<key>highlight</key>";
		    _Placemark +="<styleUrl>#sh_LineStyle</styleUrl>";
	        _Placemark +="</Pair>";
	        _Placemark +="</StyleMap>";
	        _Placemark +="<Style id=\"sn_LineStyle\">";
		    _Placemark +="<LineStyle>";
		    _Placemark +="<color>ffff00aa</color>";
		    _Placemark +="<width>5</width>";
		    _Placemark +="</LineStyle>";
		    _Placemark +="<PolyStyle>";
		    _Placemark +="<color>ff00aa00</color>";
		    _Placemark +="</PolyStyle>";
	        _Placemark +="</Style>";
            return _Placemark;
        }
        private static string GetPositionIcon(string _Color) {
            if (string.IsNullOrEmpty(_Color))
                return m_Icon;
            else
                return  "http://www.dev-poleposition.com/Images/IconGoogleEarth_"+_Color+".png";
        }
        /// <summary>
        /// REMOVES FROM THE ref _KMLItems ALL THE POLYGON TYPES & ALL THE POLYGONS
        /// </summary>        
        public static void ClearPolygonTypes(ref List<EKmlFile> KMLItems)
        {
            List<EKmlFile> _NewKMLItems = new List<EKmlFile>();
            foreach (EKmlFile _KMLItem in KMLItems)
            {
                if (_KMLItem.TypeKMLElement != TypeKMLOfElement.Polygon && 
                    _KMLItem.TypeKMLElement != TypeKMLOfElement.Style)
                {
                    _NewKMLItems.Add(_KMLItem);
                }
            }
            KMLItems = _NewKMLItems;
        }
        /// <summary>
        /// REMOVES FROM THE ref _KMLItems ALL THE DIRECTIONS
        /// </summary>
        public static void ClearDirectionsTypes(ref List<EKmlFile> KMLItems)
        {
            List<EKmlFile> _NewKMLItems = new List<EKmlFile>();
            foreach (EKmlFile _KMLItem in KMLItems)
            {
                if (_KMLItem.TypeKMLElement != TypeKMLOfElement.Direction)
                    _NewKMLItems.Add(_KMLItem);
            }
            KMLItems = _NewKMLItems;
        }
        /// <summary>
        /// REMOVES FROM THE ref _KMLItems ALL THE POINTS
        /// </summary>
        public static void ClearPointTypes(ref List<EKmlFile> KMLItems)
        {
            List<EKmlFile> _NewKMLItems = new List<EKmlFile>();
            foreach (EKmlFile _KMLItem in KMLItems)
            {
                if (_KMLItem.TypeKMLElement != TypeKMLOfElement.Point)
                    _NewKMLItems.Add(_KMLItem);
            }
            KMLItems = _NewKMLItems;
        }
        /// <summary>
        /// REMOVES FROM THE ref _KMLItems ALL THE LINES BETWEEN POINTS (RELATED WITH POSITION PARENT)
        /// </summary>
        public static void ClearLinesTypes(ref List<EKmlFile> KMLItems)
        {
            List<EKmlFile> _NewKMLItems = new List<EKmlFile>();
            foreach (EKmlFile _KMLItem in KMLItems)
            {
                if (_KMLItem.TypeKMLElement != TypeKMLOfElement.Line)
                    _NewKMLItems.Add(_KMLItem);
            }
            KMLItems = _NewKMLItems;
        }
        public static void UpdateCurrentPositon(ref List<EKmlFile> KMLItems, double Altitude, double Latitude, double Longitude, double Range, double Tilt, string AltitudeMode)
        {
            List<EKmlFile> _NewKMLItems = new List<EKmlFile>();
            foreach (EKmlFile _KMLItem in KMLItems)
            {
                if (_KMLItem.TypeKMLElement != TypeKMLOfElement.CurrentPosition)
                    _NewKMLItems.Add(_KMLItem);
            }
            EKmlFile _KMLPosition= new EKmlFile();
            _KMLPosition.GUID = "1";
            _KMLPosition.KMLCode = GenerateCurrentPosition(Altitude, Latitude, Longitude, Range, Tilt, AltitudeMode);
            _KMLPosition.TypeKMLElement = TypeKMLOfElement.CurrentPosition;
            _NewKMLItems.Add(_KMLPosition);
            KMLItems = _NewKMLItems;
        }
        private static string GenerateCurrentPosition(double Altitude, double Latitude, double Longitude, double Range, double Tilt, string AltitudeMode){
            string _LookAt = "        <LookAt id=\"ID\">";
            _LookAt += "<longitude>" + Longitude.ToString().Replace(",",".") + "</longitude>";
            _LookAt += "<latitude>" + Latitude.ToString().Replace(",", ".") + "</latitude>";
            _LookAt += "<altitude>" + Altitude.ToString().Replace(",", ".") + "</altitude>";
            _LookAt += "<range>" + Range.ToString().Replace(",", ".") + "</range>";
            _LookAt += "<tilt>" + Tilt.ToString().Replace(",", ".") + "</tilt>";
            _LookAt += "<altitudeMode>" + AltitudeMode + "</altitudeMode>";
            _LookAt += "</LookAt>";
            return _LookAt;
        }

    }
}
